/**
获取雪球网日数据
**/
package xueqiu

import (
	"encoding/json"
	"fmt"
	"io/ioutil"
	"net/http"
	"strconv"
	"time"

	"github.com/shopspring/decimal"
)

type JsonData struct {
	Data DataItem
}

type DataItem struct {
	Column []string        `json:"column"`
	Item   [][]interface{} `json:"item"`
	Symbol string          `json:"symbol"`
}

//数据库表
type Snowball struct {
	Id   int
	Code string
}

type StockDay struct {
	Id              int
	SnowballId      int
	DayQuantity     float64
	DayOpeningPrice float64
	DayMaxPrice     float64
	DayMinPrice     float64
	DayClosePrice   float64
	DayMaxMinPrice  float64
	DayMaxMinRange  float64
	DayTurnoverRate float64
	DayTotalPrice   float64
	DayDate         string
}

func GetDayData() {

	infoUrl := "https://xueqiu.com/S/SH600031"
	client := &http.Client{}
	var req *http.Request

	req, err := http.NewRequest("GET", infoUrl, nil)
	if err != nil {
		fmt.Println(err)
	}
	// cookie1 := &http.Cookie{Name: "Cookie", Value: "device_id=c92012d02156b17983b58c25fc66f95e; s=cf12b5ssc3; __utmz=1.1626008984.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_1db88642e346389874251b5a1eded6e3=1626007578,1626488486; __utmc=1; remember=1; xq_a_token=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xqat=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjMyNzgyNzI0MjcsImlzcyI6InVjIiwiZXhwIjoxNjI5MTAxNTY5LCJjdG0iOjE2MjY1MDk1Njk0NjksImNpZCI6ImQ5ZDBuNEFadXAifQ.TNaMl-WcLt1i2DGGfH6301CRF_nVt43JFQM-onqHGdFI3ebRoSgftxnUhIZTtgqvXaHgmqsNyzxzJIjNzdAFy3oxfRCR5rIeKQctYJmcZ37SzcFRASKYPklpLZ9PPxNILCIBkI7wGSmqLS1b2rLs9W_ZDbKiA34XW6FtBwz9WMpX-TpaXiHJl7Bl7vlQXSo_mbNYRV83hLwzjzw8tjV4Q7OasjzxjPoKVcPOsRjcpUSbxkb0MoeP-Ul3lT4FP4goqXueoPBXCVb-Kk_4witHvyQOvUYjPrt0PoCkNvPDIxTH5HLmvBSqora7fBGHINV-QJU1cKYUcPzdO5Rgh34AZA; xq_r_token=9f5f22224e0f025f31ded18f6e302f13f01fae08; xq_is_login=1; u=3278272427; bid=0bc32ddaa0d3d0a4adc7836f5359abcb_kr7htaxw; __utma=1.1959968610.1626008984.1626488546.1626509590.3; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1626511680; acw_tc=2760820916265250386162891ef13eb5585c82c655e0b026986ec6c3b97461; is_overseas=0", HttpOnly: true}
	// req.AddCookie(cookie1)
	req.Header.Add("Cookie", "device_id=c92012d02156b17983b58c25fc66f95e; s=cf12b5ssc3; __utmz=1.1626008984.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_1db88642e346389874251b5a1eded6e3=1626007578,1626488486; __utmc=1; remember=1; xq_a_token=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xqat=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjMyNzgyNzI0MjcsImlzcyI6InVjIiwiZXhwIjoxNjI5MTAxNTY5LCJjdG0iOjE2MjY1MDk1Njk0NjksImNpZCI6ImQ5ZDBuNEFadXAifQ.TNaMl-WcLt1i2DGGfH6301CRF_nVt43JFQM-onqHGdFI3ebRoSgftxnUhIZTtgqvXaHgmqsNyzxzJIjNzdAFy3oxfRCR5rIeKQctYJmcZ37SzcFRASKYPklpLZ9PPxNILCIBkI7wGSmqLS1b2rLs9W_ZDbKiA34XW6FtBwz9WMpX-TpaXiHJl7Bl7vlQXSo_mbNYRV83hLwzjzw8tjV4Q7OasjzxjPoKVcPOsRjcpUSbxkb0MoeP-Ul3lT4FP4goqXueoPBXCVb-Kk_4witHvyQOvUYjPrt0PoCkNvPDIxTH5HLmvBSqora7fBGHINV-QJU1cKYUcPzdO5Rgh34AZA; xq_r_token=9f5f22224e0f025f31ded18f6e302f13f01fae08; xq_is_login=1; u=3278272427; bid=0bc32ddaa0d3d0a4adc7836f5359abcb_kr7htaxw; __utma=1.1959968610.1626008984.1626488546.1626509590.3; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1626511680; acw_tc=2760820916265250386162891ef13eb5585c82c655e0b026986ec6c3b97461; is_overseas=0")
	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		fmt.Println("网络未正常响应httpCode:")
		fmt.Println(res.StatusCode)
	}

	stockBuyList := []StockBuy{}
	Db.Table("snowball").Find(&stockBuyList)

	for _, v := range stockBuyList {

		url := "https://stock.xueqiu.com/v5/stock/chart/kline.json?symbol=" + v.Code + "&begin=1626597511214&period=week&type=before&count=-365&indicator=kline,pe,pb,ps,pcf,market_capital,agt,ggt,balance"
		getDayJson(url, v.Code)
	}

}

func getDayJson(url string, code string) {
	sb := Snowball{}
	Db.Table("snowball").Where("code = ?", code).Find(&sb)

	client := &http.Client{}
	var req *http.Request

	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		fmt.Println(err)
	}
	req.Header.Add("Cookie", "device_id=c92012d02156b17983b58c25fc66f95e; s=cf12b5ssc3; __utmz=1.1626008984.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none); Hm_lvt_1db88642e346389874251b5a1eded6e3=1626007578,1626488486; __utmc=1; remember=1; xq_a_token=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xqat=19ca355c7aecac8d7d6a7077bcaa12ad7dfb12f8; xq_id_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiJ9.eyJ1aWQiOjMyNzgyNzI0MjcsImlzcyI6InVjIiwiZXhwIjoxNjI5MTAxNTY5LCJjdG0iOjE2MjY1MDk1Njk0NjksImNpZCI6ImQ5ZDBuNEFadXAifQ.TNaMl-WcLt1i2DGGfH6301CRF_nVt43JFQM-onqHGdFI3ebRoSgftxnUhIZTtgqvXaHgmqsNyzxzJIjNzdAFy3oxfRCR5rIeKQctYJmcZ37SzcFRASKYPklpLZ9PPxNILCIBkI7wGSmqLS1b2rLs9W_ZDbKiA34XW6FtBwz9WMpX-TpaXiHJl7Bl7vlQXSo_mbNYRV83hLwzjzw8tjV4Q7OasjzxjPoKVcPOsRjcpUSbxkb0MoeP-Ul3lT4FP4goqXueoPBXCVb-Kk_4witHvyQOvUYjPrt0PoCkNvPDIxTH5HLmvBSqora7fBGHINV-QJU1cKYUcPzdO5Rgh34AZA; xq_r_token=9f5f22224e0f025f31ded18f6e302f13f01fae08; xq_is_login=1; u=3278272427; bid=0bc32ddaa0d3d0a4adc7836f5359abcb_kr7htaxw; __utma=1.1959968610.1626008984.1626488546.1626509590.3; snbim_minify=true; Hm_lpvt_1db88642e346389874251b5a1eded6e3=1626511680; acw_tc=2760820916265250386162891ef13eb5585c82c655e0b026986ec6c3b97461; is_overseas=0")

	res, err := client.Do(req)
	if err != nil {
		fmt.Println(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		fmt.Println("网络未正常响应httpCode:")
		fmt.Println(res.StatusCode)
	}

	b, _ := ioutil.ReadAll(res.Body)

	str := []byte(b)
	jsonData := JsonData{}
	err = json.Unmarshal(str, &jsonData)
	if err != nil {
		fmt.Println(err)
	}

	for _, v := range jsonData.Data.Item {
		str := fmt.Sprintf("%v", v[0])
		times, err := decimal.NewFromString(str)
		if err != nil {
			continue
		}

		str2 := times.String()
		times2 := str2[:len(str2)-3]

		timeUnix, _ := strconv.ParseInt(times2, 10, 64)
		tm := time.Unix(timeUnix, 0)

		dataTime := tm.Format("2006-01-02")

		//成交量
		v1 := v[1]
		//开盘价
		v2 := v[2]
		//最高价
		v3 := v[3]
		//最低价
		v4 := v[4]
		//收盘价
		v5 := v[5]
		//涨跌额
		v6 := v[6]
		//涨跌幅
		v7 := v[7]
		//换手率
		v8 := v[8]
		//成交额
		v9 := v[9]

		sd := StockDay{}
		sd.SnowballId = sb.Id

		sd.DayQuantity = v1.(float64)
		sd.DayOpeningPrice = v2.(float64)
		sd.DayMaxPrice = v3.(float64)
		sd.DayMinPrice = v4.(float64)
		sd.DayClosePrice = v5.(float64)
		sd.DayMaxMinPrice = v6.(float64)
		sd.DayMaxMinRange = v7.(float64)
		sd.DayTurnoverRate = v8.(float64)
		sd.DayTotalPrice = v9.(float64)
		sd.DayDate = dataTime
		Db.Table("stock_week").Create(&sd)

	}

}
